package com.bjut.vendormgrsys.web.rest;

import com.bjut.vendormgrsys.service.ResumeService;
import com.bjut.vendormgrsys.support.bo.UserBO;
import com.bjut.vendormgrsys.web.dto.other.Body2;
import com.bjut.vendormgrsys.web.dto.other.Body3;
import com.bjut.vendormgrsys.web.dto.other.ResultResumeInfo;
import com.bjut.vendormgrsys.web.dto.other.ResultResumeInfoList;
import com.bjut.vendormgrsys.web.dto.other.ResultResumeInfoListData;
import com.bjut.vendormgrsys.web.dto.ResultStatusList;
import com.bjut.vendormgrsys.web.dto.other.ResumeInfo;
import com.bjut.vendormgrsys.web.dto.other.ResumeSearch;
import com.bjut.vendormgrsys.web.dto.Status;
import com.bjut.vendormgrsys.web.dto.Success;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
public class ResumeApiController implements ResumeApi {

    private static final Logger log = LoggerFactory.getLogger(ResumeApiController.class);

    private final ResumeService service;

    @org.springframework.beans.factory.annotation.Autowired
    public ResumeApiController(ResumeService service) {
        this.service = service;
    }

    @Secured({"ROLE_ADMIN", "ROLE_HRBP"})
    public ResponseEntity<Success> resumeChangeStatusPost(@Parameter(in = ParameterIn.DEFAULT, schema=@Schema()) @Valid @RequestBody List<Body3> body) {
        ResponseEntity<Success> successEntity;
        try {
            service.updateResumePOByStatus(body);
            successEntity = new ResponseEntity<>(new Success().code(HttpStatus.OK.value()).message("简历状态变更成功"), HttpStatus.OK);
        } catch (Exception e) {
            log.error("修改简历状态异常：", e);
            successEntity = new ResponseEntity<>(new Success().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).message("简历状态变更失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return successEntity;
    }

    @Secured({"ROLE_ADMIN", "ROLE_VENDOR"})
    public ResponseEntity<Success> resumeCreatePost(@Parameter(in = ParameterIn.DEFAULT, required = true, schema = @Schema()) @Valid @RequestBody ResumeInfo body) {
        ResponseEntity<Success> successEntity;
        try {
            service.validResumeInfo(body);
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            UserBO creator = (UserBO) authentication.getPrincipal();
            service.saveResumePO(body, creator.getId());
            successEntity = new ResponseEntity<>(new Success().code(HttpStatus.OK.value()).message("保存简历成功"), HttpStatus.OK);
        } catch (Exception e) {
            log.error("保存简历异常：", e);
            successEntity = new ResponseEntity<>(new Success().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).message("保存简历失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return successEntity;
    }

    public ResponseEntity<ResultResumeInfo> resumeResumeInfoPost(@Parameter(in = ParameterIn.DEFAULT, required = true, schema = @Schema()) @Valid @RequestBody Body2 body) {
        ResponseEntity<ResultResumeInfo> responseEntity;
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            UserBO user = (UserBO) authentication.getPrincipal();
            ResumeInfo data = service.findById(body.getId(), user);
            responseEntity = new ResponseEntity<>(new ResultResumeInfo().code(HttpStatus.OK.value()).message("成功").data(data), HttpStatus.OK);
        } catch (Exception e) {
            log.error("查询简历详情异常：", e);
            responseEntity = new ResponseEntity<>(new ResultResumeInfo().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).message("查询简历详情失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return responseEntity;
    }

    public ResponseEntity<ResultResumeInfoList> resumeResumeListPost(@Parameter(in = ParameterIn.DEFAULT, schema=@Schema()) @Valid @RequestBody ResumeSearch body) {
        ResponseEntity<ResultResumeInfoList> responseEntity;
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            UserBO user = (UserBO) authentication.getPrincipal();
            List<ResumeInfo> items = new ArrayList<>();
            long totalCount = service.findAll(body, items, user);
            responseEntity = new ResponseEntity<>(new ResultResumeInfoList().code(HttpStatus.OK.value()).message("成功").data(new ResultResumeInfoListData().totalCount(totalCount).items(items)), HttpStatus.OK);
        } catch (Exception e) {
            log.error("查询简历列表异常：", e);
            responseEntity = new ResponseEntity<>(new ResultResumeInfoList().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).message("查询简历列表失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return responseEntity;
    }

    public ResponseEntity<ResultStatusList> resumeStatusListGet() {
        ResponseEntity<ResultStatusList> responseEntity;
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            UserBO user = (UserBO) authentication.getPrincipal();
            List<Status> data = service.findAllStatus(user);
            responseEntity = new ResponseEntity<>(new ResultStatusList().code(HttpStatus.OK.value()).message("成功").data(data), HttpStatus.OK);
        } catch (Exception e) {
            log.error("查询简历状态列表异常：", e);
            responseEntity = new ResponseEntity<>(new ResultStatusList().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).message("查询简历状态列表失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return responseEntity;
    }
}
